let baseURL  = process.env.NODE_ENV == 'production' ? 'http://localhost:9999' : ''

function http(options){
  let {url,method='GET',data={}} = options;
  let obj = {
    method
  };
  url = baseURL + url;
  let str = '';
  Object.keys(data).forEach(key=>{
    str += `${key}=${data[key]}&`
  })
  str = str.replace(/&$/,'');
  if(/get/i.test(method)){
    url += '?'+str
  }else{
    Object.assign(obj,{
      body:str,
      headers:{
        'content-type':'application/x-www-form-urlencoded'
      }
    })
  }
  return  fetch(url,obj).then(res=>res.json())
}

http.get = function(url,data){
  return http({
    url,
    data,
    method:'get'
  })
}
http.post = function(url,data){
  return http({
    url,
    data,
    method:'post'
  })
}

export default http